package com.rxyb.hongyu.service.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.dao.ThirdHongyuDictionaryMapper;
import com.rxyb.hongyu.db.po.ThirdHongyuDictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报文校验
 *
 * @author duyh
 * @date 2020/6/9
 */
@Service
public class HongyuDictionaryLogic {

    @Autowired
    private ThirdHongyuDictionaryMapper thirdHongyuDictionaryMapper;


    /**
     * 查询
     * 由于颜色状态等可能为多个，故自行截取
     *
     * @param dmlb
     * @param dmz
     * @return
     */
//    @Cacheable(cacheNames = {"dmlb-dmz"}, key = "#dmlb", unless = "#result == null")
    public String query(String dmlb, String dmz) {
        if (null == dmz) {
            return null;
        }
        int length = dmz.trim().length();
        StringBuffer sb = new StringBuffer();
        if (HongYuConstants.DMLB_CSYS.equals(dmlb) && length > 1) {
            for (int i = 0; i < length; i++) {
                sb.append(queryOne(dmlb, dmz.substring(i, i + 1)));
            }
        } else if (HongYuConstants.DMLB_SYXZ.equals(dmlb) && length > 1) {
            for (int i = 0; i < length; i++) {
                sb.append(queryOne(dmlb, dmz.substring(i, i + 1)));
            }
        } else if (HongYuConstants.DMLB_ZT.equals(dmlb) && length > 1) {
            for (int i = 0; i < length; i++) {
                sb.append(queryOne(dmlb, dmz.substring(i, i + 1)));
            }
        } else {
            return queryOne(dmlb, dmz);
        }
        return sb.toString();
    }

//    @Cacheable
    public String queryOne(String dmlb, String dmz) {
        QueryWrapper<ThirdHongyuDictionary> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdHongyuDictionary::getDmlb, dmlb);
        wrapper.lambda().eq(ThirdHongyuDictionary::getDmz, dmz);
        final ThirdHongyuDictionary thirdHongyuDictionary = thirdHongyuDictionaryMapper.selectOne(wrapper);
        if (null != thirdHongyuDictionary) {
            return thirdHongyuDictionary.getDmsm();
        }
        return null;
    }

    /**
     * 查询某一类型的所有记录
     */
//    @Cacheable
    public List<ThirdHongyuDictionary> queryList(String dmlb) {
        QueryWrapper<ThirdHongyuDictionary> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdHongyuDictionary::getDmlb, dmlb);
        return thirdHongyuDictionaryMapper.selectList(wrapper);
    }
}

